package com.neusoft.elmboot.util;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.UserService;
import com.neusoft.elmboot.service.impl.UserServiceImpl;

@Component
public class RSAUtils {

    public String[] rsa_jiami(User user) throws Exception {
        // 生成对象，包含钥匙一对
        RSA rsa = new RSA();
        // 提取公钥并转成base64编码
        String publicKeyBase64 = rsa.getPublicKeyBase64(); 
        // 提取私钥并转成base64编码
        String privateKeyBase64 = rsa.getPrivateKeyBase64();

    	// 创建一个包含两个元素的字符串数组
        String[] keysBase64 = new String[2];
        // 将公钥和私钥放入数组中
        keysBase64[0] = publicKeyBase64;
        keysBase64[1] = privateKeyBase64;
        //返回公钥
        return keysBase64;
    }
    
    public String[] rsa_jiami(Business business) throws Exception {
        // 生成对象，包含钥匙一对
        RSA rsa = new RSA();
        // 提取公钥并转成base64编码
        String publicKeyBase64 = rsa.getPublicKeyBase64(); 
        // 提取私钥并转成base64编码
        String privateKeyBase64 = rsa.getPrivateKeyBase64();

    	// 创建一个包含两个元素的字符串数组
        String[] keysBase64 = new String[2];
        // 将公钥和私钥放入数组中
        keysBase64[0] = publicKeyBase64;
        keysBase64[1] = privateKeyBase64;
        //返回公钥
        return keysBase64;
    }
    
    public String rsa_endjiemi(String privateKeyBase64,String miwen) {
    	RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKeyBase64, null);  // 放入私钥来生成对象
    	String mingwen=rsa.decryptStr(miwen, KeyType.PrivateKey);
    	return mingwen;
    }
    
    public String rsa_endjiami(String publicKeyBase64,String mingwen) {
    	RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), null, publicKeyBase64);  // 放入公钥来生成对象
    	String miwen = rsa.encryptBase64(mingwen, KeyType.PublicKey);
    	return miwen;
    }
    
    public String md5jiami(String yuan,String adition) {
    	String md5miwen=DigestUtils.md5DigestAsHex((yuan + adition).getBytes());//md5加密，手机号为盐
    	md5miwen=DigestUtils.md5DigestAsHex((md5miwen + adition).getBytes());//二次加密
    	md5miwen=DigestUtils.md5DigestAsHex((md5miwen + adition).getBytes());//三次加密
    	return md5miwen;
    }

    

 
    
}